package com.feature.core.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.comm.SignVersion;
import com.feature.core.config.properties.AliyunOssProperties;

@EnableConfigurationProperties(AliyunOssProperties.class)
@ConditionalOnProperty("aliyun.oss.endpoint")
@Configuration
public class AliyunOssConfig {

    @Autowired
    AliyunOssProperties aliyunOssProperties;

    @Bean
    public OSS ossClient() {
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create().endpoint(aliyunOssProperties.getEndpoint())
                .credentialsProvider(CredentialsProviderFactory.newDefaultCredentialProvider(aliyunOssProperties.getAccesskey(), aliyunOssProperties.getSecret()))
                .clientConfiguration(clientBuilderConfiguration).region(aliyunOssProperties.getRegion()).build();
        return ossClient;
    }

}
